package com.fk.regimen;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends FragmentActivity implements OnClickListener{

	
	private TodayFragment todayFragment;//今天
	private TiaoYangFragment tiaoYangFragment;//调养
	private MeFragment meFragment;//我
	
	private RelativeLayout todayLayout; //今天界面布局
	private RelativeLayout tiaoYangLayout;//调养界面布局
	private RelativeLayout meLayout;//我界面布局
	
	private ImageView todayImage;//在tab布局上显示今天图标的控件
	private ImageView tiaoYangImage;//调养图标
	private ImageView meImage;//我图标
	
	private TextView todayText;//今天标题
	private TextView tiaoYangText;//调养标题
	private TextView meText;//我标题
	
	//用于对Fragment进行管理
	private FragmentManager fragmentManager;
	
	String tag = "regimen";
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
      //在数据库启动时将数据库文件复制到数据库目录下
        String DB_PATH = "/data/data/com.fk.regimen/";
        String DB_NAME = "Questions.db";
      //首先判断该数据库路径是否存在，当数据库目录不存在时，创建该数据库目录
        if((new File(DB_PATH+DB_NAME).exists()) == false){
        	File dir = new File(DB_PATH);
        	if(!dir.exists()){
        		dir.mkdir();
        	}
        }
        
        
        try {
			InputStream is = getBaseContext().getAssets().open(DB_NAME);
			OutputStream os = new FileOutputStream(DB_PATH+DB_NAME);
			byte[] buffer = new byte[1024];
			int length;
			while((length = is.read(buffer)) > 0){
				os.write(buffer, 0, length);
			}
			os.flush();
			os.close();
			is.close();
			
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        		
        
        
      //初始化布局元素
        initViews();
        //fragmentManager = getFragmentManager();
        fragmentManager = getSupportFragmentManager();
        
      //第一次启动时选中第0个tab
        setTabSelection(0);
        
        
        
    }


    //根据传入的index参数来设置选中的tab页
    //index:每个tab页对应的下标，0表示今天，1表示调养，2表示我
	private void setTabSelection(int index) {
		// TODO Auto-generated method stub
		//每次选中之前先清除掉上次的选中状态
		clearSelection();
		//开启一个Fragment事务
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		//先隐藏所有的Fragment,以防止有多个Fragment显示在界面上的情况
		hideFragments(transaction);
		
		switch (index) {
		case 0:
			//当点击了今天tab是，改变控件的图片和文字颜色
			todayImage.setImageResource(R.drawable.today2);
			todayText.setTextColor(Color.rgb(32, 230, 30));
			if(todayFragment == null){
				//如果todayFragment为空，则创建一个并添加到界面上
				todayFragment = new TodayFragment();
				transaction.add(R.id.content, todayFragment);
			}else{
				//如果todayFragment不为空，则直接将它显示出来
				transaction.show(todayFragment);
			}
			
			break;
			
		case 1:
			//当点击了调养tab是，改变控件的图片和文字颜色
//			tiaoYangImage.setImageBitmap(turnToBitmap(getResources().getResourceName(R.drawable.star2)));
		    Log.e(tag, "111111111111");
			tiaoYangImage.setImageResource(R.drawable.star2);
//			tiaoYangImage.setImageDrawable(getResources().getDrawable(R.drawable.star2));
			tiaoYangText.setTextColor(Color.rgb(32, 230, 30));
			Log.e(tag, "aaaaa"+tiaoYangFragment);
			if(tiaoYangFragment == null){
				//如果tiaoYangFragment为空，则创建一个并添加到界面上
				tiaoYangFragment = new TiaoYangFragment();
				transaction.add(R.id.content, tiaoYangFragment);
			}else{
				//如果tiaoYangFragment不为空，则直接将它显示出来
				transaction.show(tiaoYangFragment);
			}
			
			break;
			
		case 2:
			//当点击了我tab是，改变控件的图片和文字颜色
			meImage.setImageResource(R.drawable.me2);
			meText.setTextColor(Color.rgb(32, 230, 30));
			if(meFragment == null){
				//如果meFragment为空，则创建一个并添加到界面上
				meFragment = new MeFragment();
				transaction.add(R.id.content, meFragment);
			}else{
				//如果meFragment不为空，则直接将它显示出来
				transaction.show(meFragment);
			}
			
			break;

		default:
			break;
			
		}
		
		transaction.commit();
	}


	//将所有的Fragment都设置为隐藏状态
	//transaction:用于对Fragment执行操作的事务
	
	private void hideFragments(FragmentTransaction transaction) {
		// TODO Auto-generated method stub
		if(todayFragment != null)
			transaction.hide(todayFragment);
		if(tiaoYangFragment != null)
			transaction.hide(tiaoYangFragment);
		if(meFragment != null)
			transaction.hide(meFragment);	
		
	}


	//清除掉所有的选中状态
	private void clearSelection() {
		todayImage.setImageResource(R.drawable.today1);
		todayText.setTextColor(Color.parseColor("#8C8C8C"));
		tiaoYangImage.setImageResource(R.drawable.star1);
		tiaoYangText.setTextColor(Color.parseColor("#8C8C8C"));
		meImage.setImageResource(R.drawable.me1);
		meText.setTextColor(Color.parseColor("#8C8C8C"));
		
	}


	private void initViews() {
		// TODO Auto-generated method stub
		todayLayout = (RelativeLayout) findViewById(R.id.today_layout);
		tiaoYangLayout = (RelativeLayout)findViewById(R.id.tiaoyang_layout);
		meLayout = (RelativeLayout) findViewById(R.id.me_layout);
		todayImage = (ImageView) findViewById(R.id.img_today);
		tiaoYangImage = (ImageView) findViewById(R.id.img_tiaoyang);
		meImage = (ImageView) findViewById(R.id.img_me);
		todayText = (TextView) findViewById(R.id.tv_today);
		tiaoYangText = (TextView) findViewById(R.id.tv_tiaoyang);
		meText = (TextView) findViewById(R.id.tv_me);
        
		todayLayout.setOnClickListener(this);
		tiaoYangLayout.setOnClickListener(this);
		meLayout.setOnClickListener(this);

	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.today_layout:
			//当点击了今天tab时，选中第1个tab
			setTabSelection(0);
			break;
		case R.id.tiaoyang_layout:
			//当点击了今天tab时，选中第1个tab
			setTabSelection(1);
			break;
		case R.id.me_layout:
			//当点击了今天tab时，选中第1个tab
			setTabSelection(2);
			break;

		default:
			break;
		}
	}

    
}
